<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="generator" content="MATLAB R2016a"><meta http-equiv="X-UA-Compatible" content="IE=edge,IE=9,chrome=1"><title>实用计算方法实验二——多项式最小二乘法拟合</title><style type="text/css">
* {margin: 0; padding: 0;}
body {text-align: start; line-height: 17.2339992523193px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Consolas, Inconsolata, Menlo, monospace; font-style: normal; font-size: 14px; font-weight: normal; text-decoration: none; white-space: normal; }
h1, h2 {font-weight: normal;}
.content { padding: 30px; }

.S0 { margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;  }
.S1 { line-height: 26.3999996185303px; min-height: 24px; white-space: pre-wrap; color: rgb(213, 80, 0); font-family: Helvetica, Arial, sans-serif; font-size: 22px; white-space: pre-wrap; margin-left: 4px; margin-top: 3px; margin-bottom: 15px; margin-right: 10px;  }
.S2 { min-height: 0px; margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;  }
.S3 { line-height: 21px; min-height: 17px; white-space: pre-wrap; font-family: Helvetica, Arial, sans-serif; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px;  }
.S4 { font-family: Helvetica, Arial, sans-serif; margin-left: 0px; margin-top: 10px; margin-bottom: 20px; margin-right: 0px;  }
.S5 { text-align: left; line-height: 21px; white-space: pre-wrap; white-space: pre-wrap; margin-left: 56px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;  }
.S6 { text-align: center; line-height: 21px; min-height: 17px; white-space: pre-wrap; font-family: Helvetica, Arial, sans-serif; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px;  }
.S7 { margin-left: 3px; margin-top: 10px; margin-bottom: 4px; margin-right: 3px;  }
.S8 { line-height: 15.5926666259766px; min-height: 18px; white-space: nowrap; font-size: 12.6666669845581px; white-space: nowrap; margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;  }
.S9 { line-height: 15.5926675796509px; min-height: 0px; white-space: pre; color: rgb(34, 139, 34); font-size: 12.6666679382324px; white-space: pre; margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 45px;  }
.S10 { line-height: 15.5926675796509px; min-height: 0px; white-space: pre; font-size: 12.6666679382324px; white-space: pre; margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 45px;  }
.S11 { margin-left: 3px; margin-top: 4px; margin-bottom: 10px; margin-right: 3px;  }
.S12 { line-height: 15.5926675796509px; min-height: 0px; white-space: pre; font-size: 12.6666679382324px; white-space: pre; margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;  }
.S13 { line-height: 15.5926675796509px; min-height: 0px; white-space: pre; color: rgb(160, 32, 240); font-size: 12.6666679382324px; white-space: pre; margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 45px;  }
.S14 { line-height: 21px; min-height: 17px; white-space: pre-wrap; font-family: Helvetica, Arial, sans-serif; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px;  }
.S15 { margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px;  }
.S16 { color: rgb(64, 64, 64); margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;  }
.S17 { margin-left: 3px; margin-top: 4px; margin-bottom: 4px; margin-right: 3px;  }
.S18 { line-height: 15.5926675796509px; min-height: 0px; white-space: pre; color: rgb(0, 0, 255); font-size: 12.6666679382324px; white-space: pre; margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;  }
.S19 { line-height: 15.5926675796509px; min-height: 0px; white-space: pre; color: rgb(0, 0, 255); font-size: 12.6666679382324px; white-space: pre; margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 45px;  }
.S20 { line-height: 15.5926675796509px; min-height: 0px; white-space: pre; color: rgb(160, 32, 240); font-size: 12.6666679382324px; white-space: pre; margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;  }
.S21 { text-align: center; line-height: 21px; min-height: 17px; white-space: pre-wrap; font-family: Helvetica, Arial, sans-serif; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px;  }

.LineNodeBlock {margin: 10px 0 10px 0;}
.LineNodeBlock+.paragraphNode {margin-top: 10px;}
.lineNode {padding-left: 10px; background-color: #F7F7F7; border-left: 1px solid #E9E9E9; border-right: 1px solid #E9E9E9;}
.inlineWrapper:first-child .lineNode,.inlineWrapper.outputs+.inlineWrapper .lineNode {padding-top: 5px; border-top: 1px solid #E9E9E9;}
.inlineWrapper:last-child .lineNode,.inlineWrapper.outputs .lineNode {padding-bottom: 5px; border-bottom: 1px solid #E9E9E9;}
.lineNode .textBox {white-space: pre;}
.outputGroup {margin: 2px 0 2px 0; padding: 2px 2px 2px 4px;}
.outputRegion {}
.outputParagraph {color: rgba(64, 64, 64, 1); padding: 10px 0 6px 17px; background: white; overflow-x: hidden;}
.inlineWrapper:last-child .outputParagraph {border-bottom-left-radius: 4px; border-bottom-right-radius: 4px;}
.outputParagraph:empty {margin: 0;}
.inlineElement .symbolicElement {}
.embeddedOutputsSymbolicElement .MathEquation {margin-top: 5px; margin-bottom: 5px;}
.embeddedOutputsSymbolicElement .MathEquation.displaySymbolicElement {margin-left: 15px;}
.embeddedOutputsSymbolicElement .MathEquation.inlineSymbolicElement {margin-left: 5px;}
.embeddedOutputsSymbolicElement {overflow-x: auto; overflow-y: hidden;}
.embeddedOutputsSymbolicElement { overflow: initial !important;}
.embeddedOutputsTextElement,.embeddedOutputsVariableStringElement {font-family: Consolas, Inconsolata, Menlo, monospace; font-size: 12px; white-space: pre; word-wrap: initial; min-height: 18px; max-height: 250px; overflow: auto;}
.textElement {padding-top: 3px;}
.embeddedOutputsTextElement.inlineElement {}
.inlineElement .textElement {}
.embeddedOutputsTextElement,.embeddedOutputsVariableStringElement { max-height: none !important; overflow: initial !important;}
.veSpecifier {}
.veContainer {}
.veSpecifierBox {height: 400px; width: 500px;}
.veSpecifier .veTable {padding-top: 3px; padding-bottom: 4px;}
.veSpecifierBox .veSpecifier .veContainer {position: relative; width: 100%; height: 370px;}
.veSpecifierBox .dijitDialogPaneContent {width: 97% !important; height: 88% !important;}
.veSpecifier .veTable .rowHeadersWrapper {padding-bottom: 0;}
.veSpecifier .veTable .scroller .variableEditorRenderers {padding-right: 3px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none;}
.veVariableValueSummary {display: inline-block;}
.veSpecifier .veTable .topHeaderWrapper,.veSpecifier .veTable .bottomRowHeaderWrapper {visibility: hidden; z-index: 0;}
.veMetaSummary {font-style: italic;}
.veSpecifier .veTable .scroller {overflow: hidden;}
.veSpecifier .veTable:hover .scroller {overflow: auto;}
.veSpecifier .veVariableName,.veSpecifier .veDimensionFont {font-family: Consolas, Inconsolata, Menlo, monospace; font-size: 12px;}
.veSpecifier .veVariableName {padding-top: 3px;}
.veSpecifier .veDimensionFont {font-style: italic; color: #9A9A9A;}
.veSpecifier .scroller::-webkit-scrollbar-track {background-color: white;}
.veSpecifier .scroller::-webkit-scrollbar-corner {background-color: white;}
.veSpecifier .veTable .topRowHeaderWrapper {border: none; background-color: #F8F9FA;}
.veSpecifier .mw_type_ListBase.showCellBorders,.veSpecifier .veTable .topHeaderWrapper,.veSpecifier .veTable .bottomRowHeaderWrapper,.veSpecifier .veTable .verticalScrollSpacer,.veSpecifier .veTable .horizontalScrollSpacer {border: none;}
.veSpecifier .veTable .dataScrollerNode {border: 1px solid #BFBFBF;}
.veSpecifier .veTable .columnHeaderNode,.veSpecifier .veTable .rowHeaderNode,.veSpecifier .veTable .dataBody {font-family: Arial; font-size: 13px;}
.veSpecifier .veTable .columnHeaderNode,.veSpecifier .veTable .rowHeaderNode {color: #7F7F7F;}
.veSpecifier .veTable .dataBody {color: #000000;}
.veSpecifier .veTable .columnHeaderNode .cell .drag,.veSpecifier .veTable .columnHeaderNode .cell .header,.veSpecifier .veTable .topHeaderWrapper,.veSpecifier .veTable .bottomRowHeaderWrapper {background-color: #F8F9FA;}
.veSpecifier .veTable .columnHeaderNode .cell .dragBorder {border-right: 1px solid #F8F9FA;}
.veSpecifier .veTable .rowHeaderNode .cell {padding-top: 3px; text-align: center; border-bottom: 1px solid #F8F9FA;}
.veSpecifier .veTable .dataBody .cell .plainText {text-align: right;}
.veSpecifier .veTable .dataBody .row .cell {border-bottom: 1px solid #E9E9E9; border-right: 1px solid #E9E9E9;}
.embeddedOutputsVariableElement {font-family: Consolas, Inconsolata, Menlo, monospace; font-size: 12px; white-space: pre-wrap; word-wrap: break-word; min-height: 18px; max-height: 250px; overflow: auto;}
.variableElement {}
.embeddedOutputsVariableElement.inlineElement {}
.inlineElement .variableElement {}
.variableValue { width: 100% !important; }
.embeddedOutputsMatrixElement {min-height: 18px; box-sizing: border-box; font-family: Consolas, Inconsolata, Menlo, monospace; font-size: 12px;}
.matrixElement .variableValue {white-space: pre; display: inline-block; vertical-align: top; overflow: hidden;}
.embeddedOutputsMatrixElement.inlineElement {}
.embeddedOutputsMatrixElement.inlineElement .topHeaderWrapper {display: none;}
.embeddedOutputsMatrixElement.inlineElement .veTable .body {padding-top: 0 !important; max-height: 100px;}
.embeddedOutputsMatrixElement.inlineElement .veVariableName {padding-top: 0;}
.inlineElement .matrixElement {max-height: 300px;}
.embeddedOutputsMatrixElement .matrixElement .valueContainer {white-space: nowrap; padding-top: 10px; margin-bottom: 3px;}
.embeddedOutputsMatrixElement .matrixElement .valueContainer .horizontalEllipsis.hide,.embeddedOutputsMatrixElement .matrixElement .verticalEllipsis.hide {display: none;}
.embeddedOutputsMatrixElement .matrixElement .valueContainer .horizontalEllipsis {margin-bottom: -3px;}
.matrixElement { max-height: none !important; }
.dijitTooltipDialog .dijitTooltipContainer .dijitTooltipContents .alertPlugin-alertMessage {min-width: 12px; max-width: 400px; max-height: 300px; overflow: auto;}
.dijitTooltipDialog .alertPlugin-alertMessage::-webkit-scrollbar {width: 11px; height: 11px;}
.dijitTooltipDialog .alertPlugin-alertMessage::-webkit-scrollbar-track {background-color: rgba(0, 0, 0, 0);}
.dijitTooltipDialog .alertPlugin-alertMessage::-webkit-scrollbar-corner {background-color: rgba(0, 0, 0, 0);}
.dijitTooltipDialog .alertPlugin-alertMessage::-webkit-scrollbar-thumb {border-radius: 7px; background-color: rgba(0, 0, 0, 0.1); border: 2px solid rgba(0, 0, 0, 0); background-clip: padding-box;}
.dijitTooltipDialog .alertPlugin-alertMessage::-webkit-scrollbar-thumb:hover {background-color: rgba(0, 0, 0, 0.2);}
.dijitTooltipDialog .alertPlugin-alertMessage::-webkit-scrollbar-thumb {background-color: rgba(0, 0, 0, 0);}
.dijitTooltipDialog .alertPlugin-alertMessage:hover::-webkit-scrollbar-thumb {background-color: rgba(0, 0, 0, 0.1);}
.dijitTooltipDialog .alertPlugin-alertMessage:hover::-webkit-scrollbar-thumb:hover {background-color: rgba(0, 0, 0, 0.2);}
.alertPlugin-alertLine {position: absolute; display: initial; width: 40px; text-align: right; cursor: text;}
.alertPlugin-onTextLine {visibility: hidden;}
.alertPlugin-hasTooltip .alertPlugin-warningImg,.alertPlugin-hasTooltip .alertPlugin-errorImg {cursor: pointer;}
.alertPlugin-isStale {-webkit-filter: opacity(0.4) grayscale(80%); filter: opacity(0.4) grayscale(80%);}
.alertPlugin-alertLine .alertPlugin-errorElement {display: inline-block; margin-right: 4px;}
.alertPlugin-errorImg {position: relative; display: inline-block; background-color: rgb(204, 55, 41); width: 14px; height: 14px; margin-top: 1px; -webkit-border-radius: 7px;}
.alertPlugin-alertLine .alertPlugin-warningElement {display: inline-block; margin-right: 3px;}
.alertPlugin-warningImg {position: relative; display: inline-block; width: 16px; height: 15px; overflow: hidden;}
.diagnosticMessage-wrapper {font-family: Consolas, Inconsolata, Menlo, monospace; font-size: 12px;}
.diagnosticMessage-wrapper.diagnosticMessage-warningType {color: rgb(255,100,0);}
.diagnosticMessage-wrapper.diagnosticMessage-warningType a {color: rgb(255,100,0); text-decoration: underline;}
.diagnosticMessage-wrapper.diagnosticMessage-errorType {color: rgb(230,0,0);}
.diagnosticMessage-wrapper.diagnosticMessage-errorType a {color: rgb(230,0,0); text-decoration: underline;}
.diagnosticMessage-wrapper .diagnosticMessage-messagePart {white-space: pre-wrap;}
.diagnosticMessage-wrapper .diagnosticMessage-stackPart {white-space: pre;}
.embeddedOutputsWarningElement{min-height: 18px; max-height: 250px; overflow: auto;}
.embeddedOutputsWarningElement.inlineElement {}
.embeddedOutputsErrorElement {min-height: 18px; max-height: 250px; overflow: auto;}
.embeddedOutputsErrorElement.inlineElement {}
.alertPlugin-warningImg {background-image: url()}
.alertPlugin-errorImg {background-image: url()}
</style></head><body><div class = "content"><div class = 'SectionBlock containment active'><h1 class = "S1"><span class = "S2">实用计算方法实验二——多项式最小二乘法拟合</span></h1><p class = "S3"><span class = "S2">姓名：林国瑞</span></p><p class = "S3"><span class = "S2">学号：9151010F0118</span></p><p class = "S3"><span class = "S2">采用mlx，即matlab的实时脚本，便于观察结果和发布过程。</span></p><ul class = "S4"><li class = "S5"><span class = "S0">二次多项式</span></li><li class = "S5"><span class = "S0">三次多项式</span></li><li class = "S5"><span class = "S0">指数函数</span></li><li class = "S5"><span class = "S0">评价拟合效果</span></li><li class = "S5"><span class = "S0">附录：Doolittle函数解矛盾方程组</span></li></ul><ul class = "S4"><li class = "S5"><span class = "S0">二次多项式</span></li></ul><p class = "S6"><span style="vertical-align:-7"><img src="" width="116" height="26" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%二次多项式 最小二乘法 解矛盾方程组 拟合</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%形如a0*1+a1*x+a2*x^2的拟合</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">clear</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">clc</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">x=[1	2	3	4	5	6	7	8	9	10	11	12	13	14	15];</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">y=[352	211	197	160	142	106	104	60	56	38	36	32	21	19	15];</span></p></div></div></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">m=size(x,2); </span><span class = "S9">%获得数据点个数</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%n=input('请输入phi(x)的个数:');</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">n=3;</span><span class = "S9">%n即为phi(x)的个数    </span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">syms </span><span class = "S13">symsx</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">f = symfun([1,symsx,symsx^2],symsx);</span><span class = "S9">%在这里可以修改phi(x)的表达式的形式与个数，如symsx^2，在建立法方程时的值为x^2，cos(symsx)则建立法方程时为cos(x)</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">expr = formula(f);</span></p></div></div><p class = "S14"><span class = "S2">表达式分别为</span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">expr(1)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsSymbolicElement" data-width="1290" style="width: 1290px;"><div class="symbolicElement"><span class="embeddedOutputsVariableElement">ans =</span><span class="MathEquation Windows inlineSymbolicElement" style="font-size: 15px;"><img src="" width="11" height="18" style="vertical-align: bottom;"></span></div></div></div></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">expr(2)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsSymbolicElement" data-width="1290" style="width: 1290px;"><div class="symbolicElement"><span class="embeddedOutputsVariableElement">ans =</span><span class="MathEquation Windows inlineSymbolicElement" style="font-size: 15px;"><img src="" width="43" height="18" style="vertical-align: bottom;"></span></div></div></div></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">expr(3)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsSymbolicElement" data-width="1290" style="width: 1290px;"><div class="symbolicElement"><span class="embeddedOutputsVariableElement">ans =</span><span class="MathEquation Windows inlineSymbolicElement" style="font-size: 15px;"><img src="" width="48.5" height="22" style="vertical-align: bottom;"></span></div></div></div></div></div><p class = "S14"><span class = "S2">构建的法方程的A与Y</span></p></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">A=zeros(m,n);     </span><span class = "S9">%构造法方程所需的A和Y</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">j=1:n  </span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        temp =subs(expr(j),symsx,x);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        temp2 = double(temp);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S18">for </span><span class = "S10">i=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            A(i,j)=temp2(i);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">A</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>A = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&n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nbsp;&nbsp;&nbsp;&nbsp;15&nbsp;&nbsp; 225<br><br></div></div></div></div></div></div></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">Y=zeros(m,1);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">k=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        Y(k,1)=y(k);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">end</span><span class = "S10">   </span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%a=(A'*A)\(A'*Y);%左除注意</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%左除即可解出a了，这里使用一下Doolittle分解法</span></p></div></div><p class = "S14"><span class = "S2">Doolittle分解法解矛盾方程组</span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">a=Doolittle(A'*A,A'*Y)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>l = </div><div>&nbsp;&nbsp; 1.000000000000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<br>&nbsp;&nbsp; 8.000000000000000&nbsp;&nbsp; 1.000000000000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<br>&nbsp;&nbsp;82.666666666666671&nbsp;&nbsp;16.000000000000000&nbsp;&nbsp; 1.000000000000000<br><br></div></div></div><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>u = </div><div>&nbsp;&nbsp; 1.0e+03 *<br><br>&nbsp;&nbsp; 0.015000000000000&nbsp;&nbsp; 0.120000000000000&nbsp;&nbsp; 1.240000000000000<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 0.280000000000000&nbsp;&nbsp; 4.480000000000000<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 4.125333333333314<br><br></div></div></div><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>a = </div><div>&nbsp;&nbsp; 1.0e+02 *<br><br>&nbsp;&nbsp; 3.478967032967037&nbsp;&nbsp;-0.511393826761475&nbsp;&nbsp; 0.019897382029735<br><br></div></div></div></div></div></div></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%误差的平方和</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">a1=zeros(n,1);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">for </span><span class = "S10">i=1:n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    a1(i,1)=a(i);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">c=(A*a1-Y);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">Q=0;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">for </span><span class = "S10">i=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    Q=Q+c(i)^2;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S19">end</span></p></div></div><p class = "S14"><span class = "S2">误差的平方和Q为</span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">Q;</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">Q1=Q</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>Q1 = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 5.948695345830643e+03<br><br></div></div></div></div></div></div></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S12">fprintf( </span><span class = "S20">'a0: %f \t a1: %f  a2: %f\n' </span><span class = "S10">, a(1), a(2), a(3)) ;</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsTextElement" data-width="1290" style="width: 1290px;"><div class="textElement">a0: 347.896703 	 a1: -51.139383&nbsp;&nbsp;a2: 1.989738</div></div></div></div></div><p class = "S14"><span class = "S2">得到的二次函数关系</span></p><p class = "S6"><span style="vertical-align:-3"><img src="" width="290.5" height="22" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S12">fprintf( </span><span class = "S20">'%f+(%f)*x+(%f)*x^2 \n' </span><span class = "S10">, a(1), a(2), a(3)) ;</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsTextElement" data-width="1290" style="width: 1290px;"><div class="textElement">347.896703+(-51.139383)*x+(1.989738)*x^2 </div></div></div></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">for </span><span class = "S10">k=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S12">yi</span><span class = "S10">(k)=a(1)+a(2)*x(k)+a(3)*x(k)^2;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">end</span><span class = "S10">   </span></p></div></div><p class = "S14"><span class = "S2">绘制散点图</span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">plot(x,y,</span><span class = "S20">'o'</span><span class = "S12">,x,yi,</span><span class = "S20">'*'</span><span class = "S10">);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">legend(</span><span class = "S20">'真值'</span><span class = "S12">,</span><span class = "S20">'二次多项式'</span><span class = "S10">)</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S12">hold </span><span class = "S20">on</span><span class = "S10">;</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsFigure" style="max-height: 800px; width: 1290px;"><div class="figureElement"><img class="figureImage" draggable="false" src=""></div></div></div></div></div><ul class = "S4"><li class = "S5"><span class = "S0">三次多项式</span></li></ul><p class = "S6"><span style="vertical-align:-7"><img src="" width="156" height="26" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%三次多项式 最小二乘法 解矛盾方程组 拟合</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%形如a0*1+a1*x+a2*x^2+a3*x^3的拟合</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">x=[1	2	3	4	5	6	7	8	9	10	11	12	13	14	15];</span></p></div></div></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">y=[352	211	197	160	142	106	104	60	56	38	36	32	21	19	15];</span></p></div></div></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">m=size(x,2); </span><span class = "S9">%获得数据点个数</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">n=4;</span><span class = "S9">%n即为phi(x)的个数    </span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">syms </span><span class = "S13">symsx</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">f = symfun([1,symsx,symsx^2,symsx^3],symsx);</span><span class = "S9">%在这里可以修改phi(x)的表达式的形式与个数，如symsx^2，在建立法方程时的值为x^2，cos(symsx)则建立法方程时为cos(x)</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">expr = formula(f);</span></p></div></div><p class = "S14"><span class = "S2">表达式分别为</span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">expr(1)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsSymbolicElement" data-width="1290" style="width: 1290px;"><div class="symbolicElement"><span class="embeddedOutputsVariableElement">ans =</span><span class="MathEquation Windows inlineSymbolicElement" style="font-size: 15px;"><img src="" width="11" height="18" style="vertical-align: bottom;"></span></div></div></div></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">expr(2)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsSymbolicElement" data-width="1290" style="width: 1290px;"><div class="symbolicElement"><span class="embeddedOutputsVariableElement">ans =</span><span class="MathEquation Windows inlineSymbolicElement" style="font-size: 15px;"><img src="" width="43" height="18" style="vertical-align: bottom;"></span></div></div></div></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">expr(3)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsSymbolicElement" data-width="1290" style="width: 1290px;"><div class="symbolicElement"><span class="embeddedOutputsVariableElement">ans =</span><span class="MathEquation Windows inlineSymbolicElement" style="font-size: 15px;"><img src="" width="48.5" height="22" style="vertical-align: bottom;"></span></div></div></div></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">expr(4)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsSymbolicElement" data-width="1290" style="width: 1290px;"><div class="symbolicElement"><span class="embeddedOutputsVariableElement">ans =</span><span class="MathEquation Windows inlineSymbolicElement" style="font-size: 15px;"><img src="" width="48.5" height="22" style="vertical-align: bottom;"></span></div></div></div></div></div><p class = "S14"><span class = "S2">构建法方程的A与Y</span></p></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">A=zeros(m,n);     </span><span class = "S9">%构造法方程所需的A和Y</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">j=1:n  </span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        temp =subs(expr(j),symsx,x);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        temp2 = double(temp);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S18">for </span><span class = "S10">i=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            A(i,j)=temp2(i);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">A    </span></p><div class="outputParagraph"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bsp;&nbsp;&nbsp;&nbsp;&nbsp; 169&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2197<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 196&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2744<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 225&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3375<br><br></div></div></div></div></div></div></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">Y=zeros(m,1);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">k=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        Y(k,1)=y(k);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S19">end</span></p></div></div><p class = "S14"><span class = "S2">Doolittle分解法解矛盾方程</span><span class = "S2">组</span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%a=(A'*A)\(A'*Y);</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">a=Doolittle(A'*A,A'*Y);</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>l = </div><div>&nbsp;&nbsp; 1.0e+02 *<br><br>&nbsp;&nbsp; 0.010000000000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<br>&nbsp;&nbsp; 0.080000000000000&nbsp;&nbsp; 0.010000000000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<br>&nbsp;&nbsp; 0.826666666666667&nbsp;&nbsp; 0.160000000000000&nbsp;&nbsp; 0.010000000000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<br>&nbsp;&nbsp; 9.600000000000000&nbsp;&nbsp; 2.254000000000000&nbsp;&nbsp; 0.240000000000001&nbsp;&nbsp; 0.010000000000000<br><br></div></div></div><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>u = </div><div>&nbsp;&nbsp; 1.0e+04 *<br><br>&nbsp;&nbsp; 0.001500000000000&nbsp;&nbsp; 0.012000000000000&nbsp;&nbsp; 0.124000000000000&nbsp;&nbsp; 1.440000000000000<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 0.028000000000000&nbsp;&nbsp; 0.448000000000000&nbsp;&nbsp; 6.311200000000000<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 0.412533333333331&nbsp;&nbsp; 9.900800000000000<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 5.728319999998808<br><br></div></div></div></div></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">a</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>a = </div><div>&nbsp;&nbsp; 1.0e+02 *<br><br>&nbsp;&nbsp; 3.914095238095376&nbsp;&nbsp;-0.793302868155895&nbsp;&nbsp; 0.062557009983493&nbsp;&nbsp;-0.001777484498073<br><br></div></div></div></div></div></div><p class = "S14"><span class = "S2"></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%误差的平方和</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">a1=zeros(n,1);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">for </span><span class = "S10">i=1:n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    a1(i,1)=a(i);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">c=(A*a1-Y);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">Q=0;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">for </span><span class = "S10">i=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    Q=Q+c(i)^2;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S19">end</span></p></div></div><p class = "S14"><span class = "S2">误差的平方和Q为</span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">Q;</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">Q2=Q</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>Q2 = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 4.138860629892981e+03<br><br></div></div></div></div></div></div><p class = "S14"><span class = "S2"></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S12">fprintf( </span><span class = "S20">'a0: %f \t a1: %f \t a2: %f \t a3: %f \n' </span><span class = "S10">, a(1), a(2), a(3) ,a(4)) ;</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsTextElement" data-width="1290" style="width: 1290px;"><div class="textElement">a0: 391.409524 	 a1: -79.330287 	 a2: 6.255701 	 a3: -0.177748 </div></div></div></div></div><p class = "S14"><span class = "S2">得到的三次函数关系</span></p><p class = "S6"><span style="vertical-align:-3"><img src="" width="380.5" height="22" /></span></p><p class = "S3"><span class = "S2"></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S12">fprintf( </span><span class = "S20">'%f+(%f)*x+(%f)*x^2+(%f)*x^3 \n' </span><span class = "S10">, a(1), a(2), a(3), a(4)) ;</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsTextElement" data-width="1290" style="width: 1290px;"><div class="textElement">391.409524+(-79.330287)*x+(6.255701)*x^2+(-0.177748)*x^3 </div></div></div></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">for </span><span class = "S10">k=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S12">yi2</span><span class = "S10">(k)=a(1)+a(2)*x(k)+a(3)*x(k)^2+a(4)*x(k)^3;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">end</span><span class = "S10">   </span></p></div></div><p class = "S14"><span class = "S2">绘制散点图</span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">plot( x,yi2,</span><span class = "S20">'p'</span><span class = "S10">);</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S12">legend(</span><span class = "S20">'真值'</span><span class = "S12">,</span><span class = "S20">'二次多项式'</span><span class = "S12">,</span><span class = "S20">'三次多项式'</span><span class = "S10">)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsFigure" style="max-height: 800px; width: 1290px;"><div class="figureElement"><img class="figureImage" draggable="false" src=""></div></div></div></div></div></div><p class = "S0"></p><div class = 'SectionBlock containment'><ul class = "S4"><li class = "S5"><span class = "S0">建立拟合效果评价标准</span></li></ul><p class = "S3"><span class = "S2">即误差平方和的大小Q，二次函数误差平方和为Q1,三次函数误差平方和为Q2，Q值越小，拟合效果越好</span></p><p class = "S3"><span class = "S2">Q1=5948.69534583064</span></p><p class = "S3"><span class = "S2">Q2=4138.86062989298</span></p><p class = "S3"><span class = "S2"></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">Q1</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>Q1 = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 5.948695345830643e+03<br><br></div></div></div></div></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">Q2</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>Q2 = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 4.138860629892981e+03<br><br></div></div></div></div></div></div></div><p class = "S0"></p><div class = 'SectionBlock containment'><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%尝试采用y=a*exp(b*x)形式的函数拟合</span></p></div></div><p class = "S21"><span style="vertical-align:-3"><img src="" width="54.5" height="22" /></span></p><p class = "S6"><span style="vertical-align:-3"><img src="" width="99.5" height="18" /></span></p><p class = "S3"><span class = "S2">令</span><span style="vertical-align:-3"><img src="" width="53.5" height="18" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">x=[1	2	3	4	5	6	7	8	9	10	11	12	13	14	15];</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">n=size(x,2);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">xsum=sum(x);</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">xva=xsum/n</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>xva = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 8<br><br></div></div></div></div></div></div><p class = "S21"><span style="vertical-align:-3"><img src="" width="36" height="18" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">y=[352	211	197	160	142	106	104	60	56	38	36	32	21	19	15];</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">u=log(y)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement scrollableOutput" data-width="1290" style="width: 1290px;"><div class="textElement"><div>u = </div><div>&nbsp;&nbsp; 5.863631175598097&nbsp;&nbsp; 5.351858133476067&nbsp;&nbsp; 5.283203728737989&nbsp;&nbsp; 5.075173815233827&nbsp;&nbsp; 4.955827057601261&nbsp;&nbsp; 4.663439094112067&nbsp;&nbsp; 4.644390899141373&nbsp;&nbsp; 4.094344562222100&nbsp;&nbsp; 4.025351690735150&nbsp;&nbsp; 3.637586159726386&nbsp;&nbsp; 3.583518938456110&nbsp;&nbsp; 3.465735902799727&nbsp;&nbsp; 3.044522437723423&nbsp;&nbsp; 2.944438979166440&nbsp;&nbsp; 2.708050201102210<br><br></div></div></div></div></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">usum=sum(u);</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">uva=usum/n</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>uva = </div><div>&nbsp;&nbsp; 4.222738185055482<br><br></div></div></div></div></div></div><p class = "S21"><span style="vertical-align:-3"><img src="" width="90.5" height="18" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">mulxu=xsum*usum</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>mulxu = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 7.600928733099867e+03<br><br></div></div></div></div></div></div><p class = "S21"><span style="vertical-align:-15"><img src="" width="219.5" height="45" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">xsum2=xsum^2</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>xsum2 = </div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 14400<br><br></div></div></div></div></div></div><p class = "S21"><span style="vertical-align:-15"><img src="" width="114" height="45" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">xu=x.*u;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">xusum=sum(xu);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">x2=x.*x;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">x2sum=sum(x2);</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">b=(n*xusum-mulxu)/(n*x2sum-xsum2)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>b = </div><div>&nbsp;&nbsp;-0.217687176024111<br><br></div></div></div></div></div></div><p class = "S21"><span style="vertical-align:-33"><img src="" width="127" height="76" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">a1=uva-b*xva</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>a1 = </div><div>&nbsp;&nbsp; 5.964235593248374<br><br></div></div></div></div></div></div><p class = "S21"><span style="vertical-align:-3"><img src="" width="83.5" height="18" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%scatter(x,u,x,b*x+a1)</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">a=exp(a1)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>a = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 3.892553648884338e+02<br><br></div></div></div></div></div></div><p class = "S14"><span class = "S2">得到</span></p><p class = "S6"><span style="vertical-align:-3"><img src="" width="266.5" height="22" /></span></p><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">x=[1	2	3	4	5	6	7	8	9	10	11	12	13	14	15];</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">y=[352	211	197	160	142	106	104	60	56	38	36	32	21	19	15];</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">m=size(x,2); </span><span class = "S9">%获得数据点个数</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">n=1;    </span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">syms </span><span class = "S13">symsx</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">f = symfun(</span><span class = "S12">[</span><span class = "S12">a*exp(b*symsx),],symsx);</span><span class = "S9">%在这里可以修改phi(x)的表达式，如symsx^2，在建立法方程时的值为x^2，cos(symsx)则建立法方程时为cos(x)</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">expr = formula(f);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">A=zeros(m,n);     </span><span class = "S9">%构造法方程所需的A和Y</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">j=1:n  </span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        temp =subs(expr(j),symsx,x);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        temp2 = double(temp);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S18">for </span><span class = "S10">i=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            A(i,j)=temp2(i);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">Y=zeros(m,1);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">k=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        Y(k,1)=y(k);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">end</span><span class = "S10">   </span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%a=(A'*A)\(A'*Y);%左除注意</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%左除即可解出a了，这里使用一下Doolittle分解法</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">d=Doolittle(A'*A,A'*Y);</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>l = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 1<br><br></div></div></div><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>u = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 2.773369199466112e+05<br><br></div></div></div></div></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S9">%误差的平方和</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">d1=zeros(n,1);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">for </span><span class = "S10">i=1:n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    d1(i,1)=d(i);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">c=(A*d1-Y);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">Q=0;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S18">for </span><span class = "S10">i=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    Q=Q+c(i)^2;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S19">end</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S10">Q3=Q</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsVariableStringElement" data-width="1290" style="width: 1290px;"><div class="textElement"><div>Q3 = </div><div>&nbsp;&nbsp;&nbsp;&nbsp; 3.806082127183518e+03<br><br></div></div></div></div></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">plot(a*exp(b*x),</span><span class = "S20">'+'</span><span class = "S10">);</span></p></div><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S12">legend(</span><span class = "S20">'真值'</span><span class = "S12">,</span><span class = "S20">'二次多项式'</span><span class = "S12">,</span><span class = "S20">'三次多项式'</span><span class = "S12">,</span><span class = "S20">'指数函数'</span><span class = "S10">)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsFigure" style="max-height: 800px; width: 1290px;"><div class="figureElement"><img class="figureImage" draggable="false" src=""></div></div></div></div></div><p class = "S14"><span class = "S2">Q3=3806.08212718352</span></p><p class = "S3"><span class = "S2">即误差平方和的大小Q，Q值越小，拟合效果越好</span></p><p class = "S3"><span class = "S2">Q1=5948.69534583064</span></p><p class = "S3"><span class = "S2">Q2=4138.86062989298</span></p><p class = "S3"><span class = "S2">则由Q1&gt;Q2&gt;Q3</span></p><p class = "S3"><span class = "S2">得到使用指数函数拟合效果是最好的。</span></p><ul class = "S4"><li class = "S5"><span class = "S0">附录：Doolittle函数</span></li></ul><div class = 'LineNodeBlock contiguous'><div class = 'inlineWrapper outputs'><p class = "S8 lineNode"><span class = "S18">function</span><span class = "S18"> </span><span class = "S10">[x,y,l,u] = Doolittle(A,d)</span></p><div class="outputParagraph"><div class="inlineElement embeddedOutputsErrorElement" data-width="1290" style="width: 1290px;"><div class="diagnosticMessage-wrapper diagnosticMessage-errorType"><div class="diagnosticMessage-messagePart">脚本中的所有函数都必须以 'end' 结束。<br></div><div class="diagnosticMessage-stackPart"></div></div></div></div></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    n=size(A,1);</span><span class = "S9">%获得矩阵A的行数</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    n1=size(A,2);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    m=size(d,1);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">i=1:m</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S12">b</span><span class = "S10">(i)=d(i,1);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S9">%这里b是一个数组</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    u=zeros(n);</span><span class = "S9">%生成n*n的全零矩阵</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    a=zeros(n);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">i=1:n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S18">for </span><span class = "S10">j=1:n1</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            a(i,j)=A(i,j);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    l=zeros(n);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">i=1:n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        l(i,i)=1;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">end</span><span class = "S10">   </span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">k=1:n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S18">for </span><span class = "S10">j=k:n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            sum1=0;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">            </span><span class = "S18">for </span><span class = "S10">r=1:k-1</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">                sum1=sum1+l(k,r)*u(r,j);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">            </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            u(k,j)=a(k,j)-sum1;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S18">for </span><span class = "S10">i=k+1:n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            sum2=0;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">            </span><span class = "S18">for </span><span class = "S10">r=1:k-1</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">                sum2=sum2+l(i,r)*u(r,k);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">            </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            l(i,k)=(a(i,k)-sum2)/u(k,k);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    l</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">    u</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S10">i=1:n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        sum3=0;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S18">for </span><span class = "S10">j=1:(i-1)</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            sum3=sum3+l(i,j)*y(j);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S12">y</span><span class = "S10">(i)=b(i)-sum3;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S18">for </span><span class = "S12">i=n:-1:1</span><span class = "S9">%注意matlab循环变量的步进是要注明正负的</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        sum4=0;</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S18">for </span><span class = "S10">j=(i+1):n</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">            sum4=sum4+u(i,j)*x(j);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">        </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S10">        x(i)=(y(i)-sum4)/u(i,i);</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S12">    </span><span class = "S19">end</span></p></div><div class = 'inlineWrapper'><p class = "S8 lineNode"><span class = "S19">end</span></p></div></div></div></div>
<!-- 
##### SOURCE BEGIN #####
%% 实用计算方法实验二——多项式最小二乘法拟合
% 姓名：林国瑞
% 
% 学号：9151010F0118
% 
% 采用mlx，即matlab的实时脚本，便于观察结果和发布过程。
% 
% * 二次多项式
% * 三次多项式
% * 指数函数
% * 评价拟合效果
% * 附录：Doolittle函数解矛盾方程组
% 
% * 二次多项式
% 
% $$y = {a_0} + {a_1}x + {a_2}{x^2}$$

%二次多项式 最小二乘法 解矛盾方程组 拟合
%形如a0*1+a1*x+a2*x^2的拟合
clear
clc
x=[1	2	3	4	5	6	7	8	9	10	11	12	13	14	15];
y=[352	211	197	160	142	106	104	60	56	38	36	32	21	19	15];
m=size(x,2); %获得数据点个数
%n=input('请输入phi(x)的个数:');
n=3;%n即为phi(x)的个数    
syms symsx
f = symfun([1,symsx,symsx^2],symsx);%在这里可以修改phi(x)的表达式的形式与个数，如symsx^2，在建立法方程时的值为x^2，cos(symsx)则建立法方程时为cos(x)
expr = formula(f);
%% 
% 表达式分别为

expr(1)
expr(2)
expr(3)
%% 
% 构建的法方程的A与Y

A=zeros(m,n);     %构造法方程所需的A和Y
    for j=1:n  
        temp =subs(expr(j),symsx,x);
        temp2 = double(temp);
        for i=1:m
            A(i,j)=temp2(i);
        end
    end
A
Y=zeros(m,1);
    for k=1:m
        Y(k,1)=y(k);
    end   
%a=(A'*A)\(A'*Y);%左除注意
%左除即可解出a了，这里使用一下Doolittle分解法
%% 
% Doolittle分解法解矛盾方程组

a=Doolittle(A'*A,A'*Y)
%误差的平方和
a1=zeros(n,1);
for i=1:n
    a1(i,1)=a(i);
end
c=(A*a1-Y);
Q=0;
for i=1:m
    Q=Q+c(i)^2;
end
%% 
% 误差的平方和Q为

Q;
Q1=Q
fprintf( 'a0: %f \t a1: %f  a2: %f\n' , a(1), a(2), a(3)) ;
%% 
% 得到的二次函数关系
% 
% $$y = {\rm{347}}{\rm{.896703 - 51}}{\rm{.139383}}x + {\rm{1}}{\rm{.989738}}{x^2}$$

fprintf( '%f+(%f)*x+(%f)*x^2 \n' , a(1), a(2), a(3)) ;
for k=1:m
    yi(k)=a(1)+a(2)*x(k)+a(3)*x(k)^2;
end   
%% 
% 绘制散点图

plot(x,y,'o',x,yi,'*');
legend('真值','二次多项式')
hold on;
%% 
% * 三次多项式
% 
% $$y = {a_0} + {a_1}x + {a_2}{x^2} + {a_3}{x^3}$$

%三次多项式 最小二乘法 解矛盾方程组 拟合
%形如a0*1+a1*x+a2*x^2+a3*x^3的拟合
x=[1	2	3	4	5	6	7	8	9	10	11	12	13	14	15];
y=[352	211	197	160	142	106	104	60	56	38	36	32	21	19	15];
m=size(x,2); %获得数据点个数
n=4;%n即为phi(x)的个数    
syms symsx
f = symfun([1,symsx,symsx^2,symsx^3],symsx);%在这里可以修改phi(x)的表达式的形式与个数，如symsx^2，在建立法方程时的值为x^2，cos(symsx)则建立法方程时为cos(x)
expr = formula(f);
%% 
% 表达式分别为

expr(1)
expr(2)
expr(3)
expr(4)
%% 
% 构建法方程的A与Y

A=zeros(m,n);     %构造法方程所需的A和Y
    for j=1:n  
        temp =subs(expr(j),symsx,x);
        temp2 = double(temp);
        for i=1:m
            A(i,j)=temp2(i);
        end
    end
A    
Y=zeros(m,1);
    for k=1:m
        Y(k,1)=y(k);
    end
%% 
% Doolittle分解法解矛盾方程组

%a=(A'*A)\(A'*Y);
a=Doolittle(A'*A,A'*Y);
a
%% 
% 

%误差的平方和
a1=zeros(n,1);
for i=1:n
    a1(i,1)=a(i);
end
c=(A*a1-Y);
Q=0;
for i=1:m
    Q=Q+c(i)^2;
end
%% 
% 误差的平方和Q为

Q;
Q2=Q
%% 
% 

fprintf( 'a0: %f \t a1: %f \t a2: %f \t a3: %f \n' , a(1), a(2), a(3) ,a(4)) ;
%% 
% 得到的三次函数关系
% 
% $$y = 391.409524 - 79.330287x + 6.255701{x^2} - 0.177748{x^3}$$
% 
% 

fprintf( '%f+(%f)*x+(%f)*x^2+(%f)*x^3 \n' , a(1), a(2), a(3), a(4)) ;
for k=1:m
    yi2(k)=a(1)+a(2)*x(k)+a(3)*x(k)^2+a(4)*x(k)^3;
end   
%% 
% 绘制散点图

plot( x,yi2,'p');
legend('真值','二次多项式','三次多项式')
%% 
% * 建立拟合效果评价标准
% 
% 即误差平方和的大小Q，二次函数误差平方和为Q1,三次函数误差平方和为Q2，Q值越小，拟合效果越好
% 
% Q1=5948.69534583064
% 
% Q2=4138.86062989298
% 
% 

Q1
Q2
%尝试采用y=a*exp(b*x)形式的函数拟合
%% 
% $$y = a{e^{bx}}$$
% 
% $$\ln y = bx + \ln a$$
% 
% 令$u = \ln y$

x=[1	2	3	4	5	6	7	8	9	10	11	12	13	14	15];
n=size(x,2);
xsum=sum(x);
xva=xsum/n
%% 
% $$\overline x=8$$

y=[352	211	197	160	142	106	104	60	56	38	36	32	21	19	15];
u=log(y)
usum=sum(u);
uva=usum/n
%% 
% $$\overline u=4.222738$$

mulxu=xsum*usum
%% 
% $$\sum\limits_{i = 1}^n {x_i^{}} \sum\limits_{i = 1}^n {{u_i}}  = {\rm{7600}}{\rm{.92873309987}}$$

xsum2=xsum^2
%% 
% $$(\sum\limits_{i = 1}^n {{x_i}{)^2}}  = 14400$$

xu=x.*u;
xusum=sum(xu);
x2=x.*x;
x2sum=sum(x2);
b=(n*xusum-mulxu)/(n*x2sum-xsum2)
%% 
% $$b = \frac{{n\sum\limits_{i = 1}^n {{x_i}{u_i} - \sum\limits_{i = 1}^n 
% {x_i^{}} \sum\limits_{i = 1}^n {{u_i}} } }}{{n\sum\limits_{i = 1}^n {x_i^2 - 
% (\sum\limits_{i = 1}^n {{x_i}{)^2}} } }}$$

a1=uva-b*xva
%% 
% $$\ln a = \overline u - b\overline x$$

%scatter(x,u,x,b*x+a1)
a=exp(a1)
%% 
% 得到
% 
% $$y = {\rm{389}}{\rm{.255364888434}}{e^{{\rm{ - 0}}{\rm{.217687176024111}}x}}$$

x=[1	2	3	4	5	6	7	8	9	10	11	12	13	14	15];
y=[352	211	197	160	142	106	104	60	56	38	36	32	21	19	15];
m=size(x,2); %获得数据点个数
n=1;    
syms symsx
f = symfun([a*exp(b*symsx),],symsx);%在这里可以修改phi(x)的表达式，如symsx^2，在建立法方程时的值为x^2，cos(symsx)则建立法方程时为cos(x)
expr = formula(f);
A=zeros(m,n);     %构造法方程所需的A和Y
    for j=1:n  
        temp =subs(expr(j),symsx,x);
        temp2 = double(temp);
        for i=1:m
            A(i,j)=temp2(i);
        end
    end
Y=zeros(m,1);
    for k=1:m
        Y(k,1)=y(k);
    end   
%a=(A'*A)\(A'*Y);%左除注意
%左除即可解出a了，这里使用一下Doolittle分解法
d=Doolittle(A'*A,A'*Y);
%误差的平方和
d1=zeros(n,1);
for i=1:n
    d1(i,1)=d(i);
end
c=(A*d1-Y);
Q=0;
for i=1:m
    Q=Q+c(i)^2;
end
Q3=Q
plot(a*exp(b*x),'+');
legend('真值','二次多项式','三次多项式','指数函数')
%% 
% Q3=3806.08212718352
% 
% 即误差平方和的大小Q，Q值越小，拟合效果越好
% 
% Q1=5948.69534583064
% 
% Q2=4138.86062989298
% 
% 则由Q1>Q2>Q3
% 
% 得到使用指数函数拟合效果是最好的。
% 
% * 附录：Doolittle函数

function [x,y,l,u] = Doolittle(A,d)
    n=size(A,1);%获得矩阵A的行数
    n1=size(A,2);
    m=size(d,1);
    for i=1:m
        b(i)=d(i,1);
    end
    %这里b是一个数组
    u=zeros(n);%生成n*n的全零矩阵
    a=zeros(n);
    for i=1:n
        for j=1:n1
            a(i,j)=A(i,j);
        end
    end
    l=zeros(n);
    for i=1:n
        l(i,i)=1;
    end   
    for k=1:n
        for j=k:n
            sum1=0;
            for r=1:k-1
                sum1=sum1+l(k,r)*u(r,j);
            end
            u(k,j)=a(k,j)-sum1;
        end
        for i=k+1:n
            sum2=0;
            for r=1:k-1
                sum2=sum2+l(i,r)*u(r,k);
            end
            l(i,k)=(a(i,k)-sum2)/u(k,k);
        end
    end
    l
    u
    for i=1:n
        sum3=0;
        for j=1:(i-1)
            sum3=sum3+l(i,j)*y(j);
        end
        y(i)=b(i)-sum3;
    end
    for i=n:-1:1%注意matlab循环变量的步进是要注明正负的
        sum4=0;
        for j=(i+1):n
            sum4=sum4+u(i,j)*x(j);
        end
        x(i)=(y(i)-sum4)/u(i,i);
    end
end
##### SOURCE END #####
--></body></html>